package com.imddy.tweb.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

	public static boolean isEmpty(String s) {
		if (s == null || s.length() == 0) {
			return true;
		}
		return false;
	}
	
	public static boolean isNullOrEmpty(String s) {
		if (s == null || s.length() == 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 通过正则表达式获取内容
	 * @param regex	正则表达式
	 * @param from 原字符串
	 * @return
	 */
	public static String[] regex(String regex, String from){
		Pattern pattern = Pattern.compile(regex); 
		Matcher matcher = pattern.matcher(from);
		List<String> results = new ArrayList<String>();
		while(matcher.find()){
			for (int i = 0; i < matcher.groupCount(); i++) {
				results.add(matcher.group(i+1));
			}
		}
		return results.toArray(new String[]{});
	}

	/**
	 * 将一个字符串分割成List
	 * @param string
	 * @param charset
	 * @return
	 */
	public static List spitToList(String string, String charset) {
		List list = new ArrayList<>();
		String[] stringArray = string.split(charset);
		list.addAll(Arrays.asList(stringArray));
		return list;
	}
	
	/**
	 * 重复字符串多次组成新的字符串
	 * @param src
	 * @param times
	 * @return
	 */
	public static String repeat(String src, int times) {
		String result = "";
		for(int i=0; i<times; i++) {
			result = result + src;
		}
		return result;
	}
	
	/**
	 * 左填充
	 * @param src
	 * @param length
	 * @param padSrc
	 * @return
	 */
	public static String padStart(String src, int length, String padSrc) {
		if (src.length() >= length) {
			return src;
		}
		int srcLength = src.length();
		int padStrLength = padSrc.length();
		int fillLength =length - srcLength;
		int fillTimes = (int) Math.ceil( (double)fillLength / (double)padStrLength);
		System.out.println("filllength:" + fillLength);
		System.out.println("padStrLength:" + padStrLength);
		System.out.println("fillTimes:" + fillTimes);
		String tmpFillStr = "";
		for(int i=0; i<fillTimes; i++) {
			tmpFillStr = tmpFillStr + padSrc;
		}
		System.out.println(tmpFillStr);
		String fillStr = tmpFillStr.substring(0, fillLength);
		return fillStr + src;
	}
	
	/**
	 * 右填充
	 * @param src
	 * @param length
	 * @param padSrc
	 * @return
	 */
	public static String padEnd(String src, int length, String padSrc) {
		int srcLength = src.length();
		int padStrLength = padSrc.length();
		int fillLength =length - srcLength;
		int fillTimes = (int) Math.ceil( (double)fillLength / (double)padStrLength);
		String tmpFillStr = "";
		for(int i=0; i<fillTimes; i++) {
			tmpFillStr = tmpFillStr + padSrc;
		}
		String fillStr = tmpFillStr.substring(0, fillLength);
		return src + fillStr;
	}
	
	/**
	 * 首字母大写
	 * @param src
	 * @return
	 */
	public static String firstUpper(String src) {
		return src.substring(0, 1).toUpperCase() + src.substring(1);
	}
	
	/**
	 * 首字母小写
	 * @param src
	 * @return
	 */
	public static String firstLower(String src) {
		return src.substring(0, 1).toLowerCase() + src.substring(1);
	}
	
	public static void main(String[] args) {
		String string = "/test/tt.do;/test/login.do;/teat/message.html;/teat/ppp.jsp";
		List<String> list1 = StringUtils.spitToList(string, ";");
		for (String l : list1) {
			System.out.println(l);
		}
		
		System.out.println("Repeat: " + StringUtils.repeat(string, 3));
		
		System.out.println("padStart: " + StringUtils.padStart("dxw", 10, "xnb"));
		System.out.println("padEnd: " + StringUtils.padEnd("dxw", 10, "mxn"));
		
		String string2 = "ewe.fdsd.fwdf.wdfd";
		String string3 = "Fwe.fdsd.fwdf.wdfd";
		
		System.out.println(StringUtils.firstLower(string2));
		System.out.println(StringUtils.firstUpper(string2));
		
		System.out.println(StringUtils.firstLower(string3));
		System.out.println(StringUtils.firstUpper(string3));
		
	}

}
